package com.innover.cron;

import com.innover.domain.FileInfo;
import com.innover.domain.UserRecord;
import com.innover.service.FileInfoService;
import com.innover.service.UserRecordService;
import com.innover.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 12-7-13
 * Time: 上午11:54
 */

@Repository
public class CronJob {

    @Autowired
    @Qualifier("userRecordService")
    private UserRecordService userRecordService;

    Logger LOGGER = LoggerFactory.getLogger(CronJob.class);

    public void test(){
        System.out.println("--------------Test-----------");
    }

    public void deleteFiles(){

        String sql ="SELECT * FROM user_records WHERE TO_DAYS(NOW()) - TO_DAYS(upload_date) >3";

        List ft = userRecordService.queryByNative(sql,UserRecord.class);

        if(ft.size()>0){
            for(int i=0;i<ft.size();i++){

                UserRecord userRecord = (UserRecord) ft.get(i);

                FileUtil.deleteRealFile(userRecord.getRecordPath());

                LOGGER.info("Delete file :"+userRecord.getId());

            }

        }


        String delsql ="DELETE  FROM user_records WHERE TO_DAYS(NOW()) - TO_DAYS(upload_date) >3";

        Integer res = userRecordService.execteNativeBulk(delsql);

        LOGGER.info("Data delte from DB :"+res);

    }
}
